/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package github.hellocsl.gallerylayoutmanager;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import java.util.Optional;

public class MainAbility extends FractionAbility {
    public static final int SHOW_PAGER = 1;
    public static final int SHOW_GALLERY = 2;

    private MainFraction mainFraction;
    private TestFraction testFraction;
    private ViewPagerFraction viewPagerFraction;
    private final static String TAG_TEST_FRACTION = "tagTestFraction";
    private final static String TAG_VIEWPAGER_FRACTION = "tagViewPagerFraction";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#607d8b"));

        mainFraction = new MainFraction();

        getFractionManager()
                .startFractionScheduler()
                .add(ResourceTable.Id_sl_main, mainFraction)
                .submit();
    }

    public void gotoFraction(int type) {
        switch (type) {
            case SHOW_GALLERY:
                testFraction = new TestFraction();
                showFragment(testFraction, TAG_TEST_FRACTION);
                break;
            case SHOW_PAGER:
                getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
                viewPagerFraction = new ViewPagerFraction();
                showFragment(viewPagerFraction, TAG_VIEWPAGER_FRACTION);
                break;
            default:
                throw new IllegalStateException("unknown type : " + type);
        }
    }

    private void showFragment(Fraction fraction, String tag) {
        getFractionManager().startFractionScheduler()
                .add(ResourceTable.Id_sl_main, fraction, tag)
                .remove(mainFraction)
                .submit();
    }

    @Override
    protected void onBackPressed() {
        Optional<Fraction> testTag = getFractionManager().getFractionByTag(TAG_TEST_FRACTION);
        Optional<Fraction> viewPagerTag = getFractionManager().getFractionByTag(TAG_VIEWPAGER_FRACTION);
        if (testTag.isPresent()) {
            getFractionManager().startFractionScheduler()
                    .remove(testFraction)
                    .add(ResourceTable.Id_sl_main, mainFraction)
                    .submit();
        } else if (viewPagerTag.isPresent()) {
            getWindow().clearFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
            getFractionManager().startFractionScheduler()
                    .remove(viewPagerFraction)
                    .add(ResourceTable.Id_sl_main, mainFraction)
                    .submit();
        } else {
            super.onBackPressed();
        }
    }
}
